import {
  defineConfig
} from 'vite'
import vue from '@vitejs/plugin-vue2'
import { createSvgIconsPlugin } from 'vite-plugin-svg-icons'
import legacyPlugin from '@vitejs/plugin-legacy'
import path from 'path'

export default defineConfig(() => {
  return {
    base: './',
    plugins: [
      vue(),
      createSvgIconsPlugin({
        iconDirs: [path.resolve(process.cwd(), 'src/assets/svg'), path.resolve(process.cwd(), 'src/assets/icons')],
        symbolId: 'icon-[dir]-[name]'
      }),
      legacyPlugin({
        targets: ['chrome 52'], // 需要兼容的目标列表，可以设置多个
        additionalLegacyPolyfills: ['regenerator-runtime/runtime'] // 面向IE11时需要此插件
      })
    ],
    build: {
      minify: 'terser',
      terserOptions: {
        compress: {
          // 生产环境时移除console和debugger
          drop_console: true,
          drop_debugger: true
        }
      }
    },
    server: {
      host: '0.0.0.0',
      port: 9100,
      open: false,
      disableHostCheck: true,
      proxy: {
        '/api': {
          target: 'http://localhost:9000/',
          // target: 'http://www.easyboot.cn:9000/',
          logLevel: 'debug',
          ws: false,
          changeOrigin: true,
          rewrite: (path) => path.replace(/^\/api/, '')
        }
      }
    },
    // 解决element图标与sass冲突问题
    css: {
      loaderOptions: {
        sass: {
          implementation: require('sass'),
          sassOptions: {
            // 生效代码
            outputStyle: 'expanded'
          }
        }
      }
    },
    resolve: {
      /** 添加alias规则 */
      alias: [
        {
          find: '@',
          replacement: path.resolve(__dirname, 'src')
        },
        {
          find: '/@',
          replacement: 'src'
        }
      ],
      extensions: ['.vue', '.js', '.json']
    }
  }
})
